package top.heathylife.gateway.common.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 功能描述: Test
 *
 * @author maoxiaomeng
 * @date 20231122
 */
public class Test {
    static String filePath = "D:\\develop\\IdeaProjects\\dynamic-api-gateway\\dynamic-api-modules\\gateway-platform-dtos";
    static String filePath2 = "D:\\develop\\IdeaProjects\\dynamic-api-gateway - 副本\\dynamic-api-modules\\gateway-platform-dtos";

    static String tpl = "        <module>{}</module>";

    static List<String> moduleName = new ArrayList<>();

    public static void main(String[] args) {

        //List<File> files = FileUtil.loopFiles(filePath);
        //moduleName = files.stream()
        //        .filter(e -> e.getPath().contains("pom.xml"))
        //        .map(Test::extractArtifactId).filter(e-> !"gateway-platform-dtos".equalsIgnoreCase(e)).collect(Collectors.toList());
        //String res = moduleName.stream().map(e -> StrUtil.format(tpl, e)).collect(Collectors.joining(
        //        "\n"));
        //System.out.println("<modules>\n " + res + "\n </modules>");
        File file = new File(filePath);
        String[] list = file.list();
        Arrays.stream(list).filter(e -> !"pom.xml".equalsIgnoreCase(e)).forEach(e -> {
            File temp = new File(e);
            createDirectory(temp);
        });

    }


    public static void createDirectory(File file) {
        String tempDirectoryPath = StrUtil.format("{}\\src\\main\\java\\top\\healthylife{}",
                filePath+"\\"+file.getName(), "\\gateway\\platform\\"+ file.getName().replace("platform-", "").replace(
                        "-dto",
                        "") +
                        "\\dto");
        FileUtil.mkdir(tempDirectoryPath);
        System.out.println("tempDirectoryPath = " + tempDirectoryPath);
    }

    public static String extractArtifactId(File file) {
        String content = FileUtil.readString(file.getPath(), Charset.defaultCharset());
        String targetTag = "<artifactId>"; // 目标起始标签
        String targetCloseTag = "</artifactId>"; // 目标闭合标签
        int startIndex = content.indexOf(targetCloseTag);
        if (startIndex != -1) {
            content = content.substring(startIndex + targetCloseTag.length());
            startIndex = content.indexOf(targetTag);
            int endIndex = content.indexOf(targetCloseTag);
            if (endIndex != -1) {
                return content.substring(startIndex + targetTag.length(), endIndex);

            }
        }
        return "";
    }
}
